package com.ailanyin.admin.modules.system.controller;

import com.ailanyin.admin.modules.system.model.SysLoginLog;
import com.ailanyin.admin.modules.system.service.SysFileService;
import com.ailanyin.admin.modules.system.service.SysLoginLogService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.ailanyin.common.utils.ExcelUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统登录日志管理
 *
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 14:41
 */
@RestController
@RequestMapping("/monitor/logininfor")
@Api(tags = "系统登录日志管理")
public class SysLoginLogController {

    @Autowired
    private SysLoginLogService loginLogService;
    @Autowired
    private SysFileService sysFileService;

    @PreAuthorize("hasAuthority('monitor:logininfor:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取登录日志列表")
    public CommonPage list(SysLoginLog sysLoginLog,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysLoginLog> list = loginLogService.selectLoginLogList(sysLoginLog);
        return CommonPage.restPage(list);
    }

    @PreAuthorize("hasAuthority('monitor:logininfor:export')")
    @PostMapping("/export")
    @ApiOperation(value = "导出登录日志")
    public void export(HttpServletResponse response, SysLoginLog sysLoginLog) throws Exception {
        List<SysLoginLog> list = loginLogService.selectLoginLogList(sysLoginLog);
        String downloadFileName = ExcelUtil.exportExcel(list, "登录日志表", "登录日志", SysLoginLog.class, "登录日志");
        sysFileService.fileDownload(downloadFileName, response);
    }

    @PreAuthorize("hasAuthority('monitor:logininfor:remove')")
    @DeleteMapping("/{infoIds}")
    @ApiOperation(value = "删除登录日志")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return AjaxResult.toResult(loginLogService.deleteLoginLogByIds(infoIds));
    }

    @PreAuthorize("hasAuthority('monitor:logininfor:remove')")
    @DeleteMapping("/clean")
    @ApiOperation(value = "清空登录日志")
    public AjaxResult clean() {
        loginLogService.cleanLoginLog();
        return AjaxResult.success();
    }
}
